package com.wind.taobao.api.trade.shop.convert
{
	import com.wind.taobao.api.trade.shop.model.ShopCat;
	import com.wind.taobao.core.util.TaobaoConstants;

	import mx.collections.ArrayCollection;


	/**
	 * ShopCat转换器
	 * @author fxy
	 *
	 */
	public class ShopCatConvert
	{
		/**
		 * 转换ShopCat集合
		 */
		public static function convertShopCats(value:Object, type:String="json"):ArrayCollection
		{
			var shopCats:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							shopCats.addItem(convertShopCat((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return shopCats;
		}

		/**
		 * 转换ShopCat
		 */
		public static function convertShopCat(value:Object, type:String="json"):ShopCat
		{
			var shopCat:ShopCat=new ShopCat();
			if (value == null)
				return shopCat;
			if (type == TaobaoConstants.XML)
			{
				shopCat=convertXMLToShopCat(XML(value), shopCat);
			}
			else
			{
				shopCat=convertJSONToShopCat(value, shopCat);
			}
			return shopCat;
		}

		/**
		 * JSON转换为ShopCat
		 */
		private static function convertJSONToShopCat(json:Object, shopCat:ShopCat):ShopCat
		{
			if (json.hasOwnProperty(TaobaoConstants.CID))
			{
				shopCat.cid=json[TaobaoConstants.CID];
			}
			if (json.hasOwnProperty(TaobaoConstants.PARENT_CID))
			{
				shopCat.parent_cid=json[TaobaoConstants.PARENT_CID];
			}
			if (json.hasOwnProperty(TaobaoConstants.NAME))
			{
				shopCat.name=json[TaobaoConstants.NAME];
			}
			if (json.hasOwnProperty(TaobaoConstants.IS_PARENT))
			{
				shopCat.is_parent=json[TaobaoConstants.IS_PARENT];//已转换Boolean
			}
			return shopCat;
		}

		/**
		 * XML转换为ShopCat
		 */
		private static function convertXMLToShopCat(value:XML, shopCat:ShopCat):ShopCat
		{
			return shopCat;
		}

	}
}